<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="title" :visible="showDialog" @close="NOclick">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form label-width="120px" :model="formData" :rules="rules" ref="forms">
      <el-form-item label="部门名称" prop="name">
        <el-input
          style="width: 80%"
          placeholder="1-50个字符"
          v-model="formData.name"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          style="width: 80%"
          placeholder="1-50个字符"
          v-model="formData.code"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          style="width: 80%"
          placeholder="请选择"
          v-model="formData.manager"
          @focus="getSimple"
        >
          <el-option
            v-for="item in simpleList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
          v-model="formData.introduce"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" @click="OKClick" size="small">确定</el-button>
        <el-button size="small" @click="NOclick">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import {
  getDepartment,
  addDepartments,
  editDepartments,
  setDepartments,
} from "@/api/department.js";
import { getSimple } from "@/api/employees.js";
export default {
  data() {
    // 判断部门名称是否重复
    let checkName = async (rule, value, callback) => {
      let res = await getDepartment();
      let falg;
      if (this.formData.id) {
        falg = res.depts
          .filter((item) => item.pid == this.formData.pid)
          .some((val) => val.name == value && val.name != this.oldForm.name);
      } else {
        falg = res.depts
          .filter((item) => item.pid == this.pid.id)
          .some((item) => item.name == value);
      }
      falg
        ? callback(new Error(`同级部门下已经有《${value}》这个部门了`))
        : callback();
    };

    // 判断部门编码是否重复
    let checkCode = async (rule, value, callback) => {
      let res = await getDepartment();
      let falg;
      if (this.formData.id) {
        falg = res.depts.some(
          (item) => item.code == value && item.code != this.oldForm.code
        );
      } else {
        falg = res.depts.some((item) => item.code == value && value);
      }
      falg ? callback(new Error(`这个《${value}》部门编码已存在`)) : callback();
    };
    return {
      formData: {
        name: "", // 部门名称
        code: "", // 部门编码
        manager: "", // 部门管理者
        introduce: "", // 部门介绍
      },
      // 保存编辑信息的部门
      oldForm: {
        name: "",
        code: "",
      },
      simpleList: {},
      // 定义校验规则
      rules: {
        name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "部门名称要求1-50个字符",
            trigger: "blur",
          },
          { validator: checkName, trigger: "blur" },
        ],
        code: [
          { required: true, message: "部门编码不能为空", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "部门编码要求1-50个字符",
            trigger: "blur",
          },
          { validator: checkCode, trigger: "blur" },
        ],
        manager: [
          { required: true, message: "部门负责人不能为空", trigger: "blur" },
        ],
        introduce: [
          { required: true, message: "部门介绍不能为空", trigger: "blur" },
          {
            trigger: "blur",
            min: 1,
            max: 300,
            message: "部门介绍要求1-50个字符",
          },
        ],
      },
    };
  },

  methods: {
    async getSimple() {
      this.simpleList = await getSimple();
    },
    // 获取部门详情
    async getDepartDetail(id) {
      this.formData = await editDepartments(id);
      this.oldForm.name = this.formData.name;
      this.oldForm.code = this.formData.code;
    },
    // 取消按钮
    NOclick() {
      this.$refs.forms.resetFields();
      this.$emit("update:showDialog", false);
    },
    // 确定按钮
    OKClick() {
      this.$refs.forms.validate(async (falg) => {
        if (falg) {
          if (this.formData.id) {
            await setDepartments(this.formData)
            this.$message.success('修改部门成功')
          } else {
            await addDepartments({ ...this.formData, pid: this.pid.id });
            this.$message.success("新增部门成功");
          }
          this.$emit("getDepartments");
          this.$emit("update:showDialog", false);
        }
      });
    },
  },
  computed: {
    title() {
      return this.formData.id ? "编辑部门" : "新增部门";
    },
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
    pid: {
      default: {},
    },
  },
};
</script>

<style>
</style>